package eliauk.fun.flowabledemo.service.impl;

import eliauk.fun.flowabledemo.service.FlowServiceFactory;
import eliauk.fun.flowabledemo.service.FlowService;
import org.flowable.engine.repository.DeploymentBuilder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.zip.ZipInputStream;

@Service
public class FlowServiceImpl extends FlowServiceFactory implements FlowService {


    /**
     * 部署
     */
    @Override
    public void deploy(String name, MultipartFile file) throws IOException {
        DeploymentBuilder deploymentBuilder=repositoryService.createDeployment();
        deploymentBuilder.name(name);
        String fileName=file.getOriginalFilename();
        if(fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn")){
            deploymentBuilder.addInputStream(file.getOriginalFilename(),file.getInputStream());
        }else if(fileName.endsWith(".bar") || fileName.endsWith(".zip")){
            deploymentBuilder.addZipInputStream(new ZipInputStream(file.getInputStream()));
        }else {
            throw new RuntimeException("文件类型不是 .bpmn20.xml 或者 .bpmn 或者 .bar 或者.zip");
        }
        //部署成功后 act_re_deployment，act_re_procdef，act_ge_bytearray 表就有数据了
        deploymentBuilder.deploy();
    }

}
